package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.common.http.PayState;
import com.qf.common.utils.Time;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.Order;
import com.qf.entity.po.LoginMerchant;
import com.qf.entity.vo.OrderVo;
import com.qf.mapper.GoodsTypeMapper;
import com.qf.mapper.OrderMapper;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {

    @Transactional(readOnly = true)
    public Integer getBuyerNumByGoodsId(Long goodsId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Order::getGoodsId,goodsId);
        return this.count(wrapper);
    }

    @Transactional
    public void createOrder(Order order, LoginMerchant loginMerchant) {
        order.setOrderId(String.format("%s%s%s",
                Time.timeTosecond(),
                loginMerchant.getId(),
                RandomUtils.nextInt(10000,100000)));
        order.setMerchantId(loginMerchant.getId());
        order.setPrice(1);
        order.setPayState(PayState.PENDING.getCode());
        this.save(order);
    }

    @Transactional(readOnly = true)
    public List<OrderVo> getOrderByMerchantUserId(LoginMerchant loginMerchant) {
        return this.baseMapper.getOrderByMerchantUserId(loginMerchant.getId());

    }
}
